package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BookDao;
import dao.BookTypeDao;
import domain.Book;
import domain.BookType;

/**
 * Servlet implementation class EditBook
 */
public class EditBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BookDao dao = new BookDao();
		Book book = dao.getByBookId(request.getParameter("bookId"));
		request.setAttribute("book", book);
		BookTypeDao bookTypeDao=new BookTypeDao();
		List<BookType> bookTypes=bookTypeDao.getAllBookTypes();
		request.setAttribute("bookTypes", bookTypes);
		RequestDispatcher rd = request
				.getRequestDispatcher("/WEB-INF/jsp/editBook.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BookTypeDao bookTypeDao=new BookTypeDao();
		BookDao bookManage=new BookDao();
		Book book=new Book();
		book.setContent(request.getParameter("content"));
		book.setName(request.getParameter("name"));
		book.setPrice(new Float(request.getParameter("price")));
		BookType bookType=bookTypeDao.getByTypeId(request.getParameter("bookTypeId"));
		book.setBookType(bookType);
		bookManage.update(new Integer(request.getParameter("bookId")), book);
		response.sendRedirect(request.getContextPath()+"/admin/ListBook");
	}

}
